/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.mall.member.web.JspUtil
   Module Description   :

   Date Created      : 2010/2/6
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.mall.util;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.opensymphony.xwork2.ActionContext;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.ec.campaign.entity.Campaign;
import com.yaodian100.ec.campaign.service.CampaignService;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.search.SearchManager;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.service.ShelfService;
import com.yaodian100.mall.member.util.UserData;

/**
 * @author andy.chuang
 * 
 */
public class JspUtil implements ApplicationContextAware, InitializingBean {
	private static ApplicationContext ctx;
	private static BaseManager baseMgr;
	private static SearchManager searchManager;
	private static ProductService productService;
	private static CampaignService campaignService;
	private static ShelfService shelfService;

	public static Boolean isLogined() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (userData.getLoginId() != null) {
			return true;
		}
		return false;
	}

	public static String memberId() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		return userData.getMemberId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.
	 * ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ctx = applicationContext;

	}

	public static String[] listHotTermsByCategory(String catLevel, String categoryL1) {
		int categoryLevel = -1;
		if (catLevel != null && catLevel.length() > 0) {
			categoryLevel = Integer.valueOf(catLevel);
		}
		String hotTerms[] = null;
		try {
			if (categoryLevel == -1) {
				hotTerms = searchManager.listHotTerm(5);
			} else {
				String catId = "";
				if (categoryL1 != null && !categoryL1.equals("0")) {
					catId = categoryL1;
				}
				hotTerms = searchManager.listHotTermsByCategory(categoryLevel, catId, 5);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return hotTerms;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		baseMgr = (BaseManager) ctx.getBean("baseMgr");
		searchManager = (SearchManager) ctx.getBean("searchManager");
		productService = (ProductService) ctx.getBean("productService");
		campaignService = (CampaignService) ctx.getBean("campaignService");
		shelfService = (ShelfService) ctx.getBean("shelfService");
	}

	public static String productBrief(String productId) throws CoreException {
		return productService.readFromFile(productId, "brief");
	}

	public static String productDescription(String productId) throws CoreException {
		return productService.readFromFile(productId, "description");
	}

	public static String productDetail(String productId) throws CoreException {
		return productService.readFromFile(productId, "details");
	}

	public static String shoppingMustRead(String rootCategory) throws CoreException {
		return productService.readFromFile(rootCategory, "orderNotice");
	}

	public static List<Campaign> validCampaign(String categoryPath, String productId) {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		Map optional = new HashMap();
		optional.put("domain", request.getServerName());

		optional.put("cookie", userData.getApplySource());

		return campaignService.getValidCampaign(categoryPath, productId, optional);
	}

	public static String getParentCategoryId(String categoryId) {
		if (categoryId == null || categoryId.length() == 0) {
			return null;
		}

		Category category = (Category) baseMgr.get(Category.class, Long.valueOf(categoryId));
		if (category == null) {
			return null;
		}
		String categoryPath = category.getCategoryPath();
		if (categoryPath != null) {
			String[] categoryIds = categoryPath.split("/");
			if (categoryIds.length >= 2) {
				String parentCategoryId = categoryIds[categoryIds.length - 2];
				return parentCategoryId;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	public static String getCategoryL1(String categoryId) {
		if (categoryId.equals("0")) {
			return categoryId;
		}
		Category category = (Category) baseMgr.get(Category.class, Long.valueOf(categoryId));
		String cps[] = category.getCategoryPath().split("/");

		return cps[0];
	}

	public static String getCategoryId(String categoryPath, String reg) {
		if (categoryPath == null) {
			return null;
		}
		String cps[] = categoryPath.split(reg);
		return cps[cps.length - 1];
	}

	public static Object[] getNavigation(String categoryId) {
		if (categoryId == null || categoryId.length() == 0) {
			return null;
		}
		Category category = (Category) baseMgr.get(Category.class, Long.valueOf(categoryId));
		if (category == null) {
			return null;
		}
		String categoryPath = category.getCategoryPath();
		String categoryIds[] = categoryPath.split("/");
		Stack<String[]> stack = new Stack<String[]>();
		for (String id : categoryIds) {
			Category cate = null;
			if (id.equals(categoryId)) {
				cate = category;
			} else {
				cate = (Category) baseMgr.get(Category.class, Long.valueOf(id));
			}
			String[] idValue = new String[2];
			idValue[0] = id;
			idValue[1] = cate.getName();
			stack.add(idValue);
		}
		return stack.toArray();
	}

	public static boolean inDomainAndChannel(String dc) {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		String applySource = userData.getApplySource();
		if (applySource == null) {
			applySource = "";
		}
		boolean s = false;
		if (dc.indexOf(request.getServerName() + "_" + applySource) > 0) {
			s = true;
		}
		if (s) {
			return true;
		} else {
			return false;
		}

	}

	public static Shelf getActiveShelf(String productId) {
		return shelfService.getActiveShelfByProductId(productId);
	}
}
